from applications.configs.global_configs import global_config
from sanic import Sanic
# from sanic_openapi import  openapi2_blueprint
from sanic_openapi import openapi3_blueprint
from applications.logs import init_logs
# from applications.api import init_api
from applications.utils import init_utils
from loguru import logger
from werkzeug.utils import ImportStringError, find_modules, import_string
import os

def create_app(config_name=None):
    """创建sanic app

    :param config_name: 默认config_name='Dev'，使用Dev配置
    :return: app -> Sanic
    """

    app = Sanic(__name__)


    if not config_name:
        # 尝试从本地环境中读取
        config_name = os.getenv('SANIC_ENV', 'DEV')
    #引入配置
    app.update_config(global_config[config_name])

    #初始化utils or plugs
    init_utils(app)
    #初始化log
    init_logs()
    #初始化路由
    register_blueprints('applications.api.leaftask',app)
    # 初始化index
    register_blueprints('applications.api.index', app)
    #加载openapi路由
    app.blueprint(openapi2_blueprint)



    #打印logo
    logo(config_name)
    return app

def register_blueprints(root, app, prefix='/'):
    # 遍历模块列表
    for name in find_modules(root, recursive=True):
        mod = import_string(name)
        # 判断是否有 bp
        if hasattr(mod, 'bp'):

            # 注册路由
            # route = Blueprint.group(mod.bp, url_prefix=prefix)
            # logger.info(route)
            logger.info('{}       ,api 初始化完成'.format(name))
            app.blueprint(mod.bp)






    
def logo(config_name):
    if config_name == "PRD":
        print("""
                                    ########  ########  ########  
                                    ##     ## ##     ## ##     ## 
                                    ##     ## ##     ## ##     ## 
                                    ########  ########  ##     ## 
                                    ##        ##   ##   ##     ## 
                                    ##        ##    ##  ##     ## 
                                    ##        ##     ## ########  
        """)
    elif config_name == "DEV":
        print("""
                                    ########  ######## ##     ## 
                                    ##     ## ##       ##     ## 
                                    ##     ## ##       ##     ## 
                                    ##     ## ######   ##     ## 
                                    ##     ## ##        ##   ##  
                                    ##     ## ##         ## ##   
                                    ########  ########    ###  
        """)
    elif config_name == "TEST":
        print("""
                                    ######## ########  ######  ######## 
                                       ##    ##       ##    ##    ##    
                                       ##    ##       ##          ##    
                                       ##    ######    ######     ##    
                                       ##    ##             ##    ##    
                                       ##    ##       ##    ##    ##    
                                       ##    ########  ######     ##    
        """)